const {
  ActivityModel
} =  require('../../../../../utils/activity-model.js')
const activityModel = new ActivityModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationHeight: 64,
    navigationTop: 26,
    navigationLeft: 10,
    capsuleWidth: 87,
    resultShow: false,
    exchangeResult: false,
    lottery: {},
    awardInfo: {},
    dust: 0,
    dustArr: [],
    dustAwards: [],
    exchangeType: 1
  },
  exchange(e) {
    const { item } = e.currentTarget.dataset
    activityModel.dustExchange({
      activityId: this.data.activityId,
      awardId: item.awardId,
      count: +item._number
    }).then(data => {
      this.setData({
        exchangeResult: true,
        dust: this.data.dust - item._number * item.exchangePrice,
        exchangeType: item.awardType
      })
      this.getDustArr()
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  closeExchangeResult() {
    this.setData({
      exchangeResult: false
    })
  },
  decrease(e) {
    const number = +e.currentTarget.dataset.item._number
    const index = e.currentTarget.dataset.index
    if (number > 1) {
      this.data.dustAwards[index]._number = number - 1 + ''
      this.setData({
        dustAwards: this.data.dustAwards
      })
    }
  },
  increase(e) {
    const number = +e.currentTarget.dataset.item._number
    const index = e.currentTarget.dataset.index
    if (number < 999) {
      this.data.dustAwards[index]._number = number + 1 + ''
      this.setData({
        dustAwards: this.data.dustAwards
      })
    }
  },
  inputNum(event) {
    const number = +event.currentTarget.dataset.item._number
    const index = event.currentTarget.dataset.index
    if (event.detail.value === '' || event.detail.value === '0') {
      wx.showToast({
        title: '请填写正确数量',
        icon: 'none'
      })
      this.data.dustAwards[index]._number = '1'
      this.setData({
        dustAwards: this.data.dustAwards
      })
    } else {
      this.data.dustAwards[index]._number = event.detail.value
      this.setData({
        dustAwards: this.data.dustAwards
      })
    }
  },
  showResult() {
    if (this.data.lottery.dustCount <= this.data.dust) { 
      activityModel.dustAward({
        activityId: this.data.activityId
      }).then(data => {
        this.setData({
          resultShow: true,
          awardInfo: data || {},
          dust: this.data.dust - this.data.lottery.dustCount
        })
        this.deleteMap()
        this.getDustArr()
        this.getAwardInfo()
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  closeResult() {
    this.setData({
      resultShow: false
    })
  },
  goback() {
    wx.navigateBack({
      fail() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    })
  },
  getDustArr() {
    this.setData({
      dustArr: (this.data.dust + '').split('')
    })
  },
  onLoad: function (options) {
    const dustAwards = JSON.parse(options.dustAwards)
    this.setData({
      dustAwards: dustAwards.filter(item => item.awardType === 1 || item.awardType === 2).map(item => {
        return {
          ...item,
          _number: '1'
        }
      }),
      activityId: options.activityId,
      dust: options.dust
    })
    this.getDustArr()
    const navigationBtnObj = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: res => {
        this.setData({
          navigationHeight: res.statusBarHeight + navigationBtnObj.height + (navigationBtnObj.top - res.statusBarHeight) * 2,
          navigationTop: navigationBtnObj.top,
          navigationLeft: res.windowWidth - navigationBtnObj.right,
          capsuleWidth: navigationBtnObj.width
        })
      }
    })
    this.getAwardInfo()
  },
  deleteMap() {
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    prevPage.deleteMap && prevPage.deleteMap()
  },
  getAwardInfo() {
    activityModel.dustModuleShow({
      activityId: this.data.activityId
    }).then(data => {
      this.setData({
        lottery: data || {}
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  }
})